﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;

namespace Enterprise.Logging
{
    public static class Logger
    {
        // Methods
        public static void Debug(object caller, object methodName, object message)
        {
            if (caller == null)
            {
                throw new ArgumentNullException("caller");
            }
            ILog logger = LogManager.GetLogger(caller.GetType());
            if ((logger != null) && logger.IsDebugEnabled)
            {
                logger.Debug(GetMessage(methodName, message));
            }
        }

        public static void Error(object caller, object methodName, object message)
        {
            if (caller == null)
            {
                throw new ArgumentNullException("caller");
            }
            ILog logger = LogManager.GetLogger(caller.GetType().ToString());
            if ((logger != null) && logger.IsErrorEnabled)
            {
                logger.Error(GetMessage(methodName, message));
            }
        }

        public static void Fatal(object caller, object methodName, object message)
        {
            if (caller == null)
            {
                throw new ArgumentNullException("caller");
            }
            ILog logger = LogManager.GetLogger(caller.GetType().ToString());
            if ((logger != null) && logger.IsFatalEnabled)
            {
                logger.Fatal(GetMessage(methodName, message));
            }
        }

        private static string GetMessage(object methodName, object message)
        {
            return new StringBuilder().Append(methodName).Append(": ").Append(message).ToString();
        }

        public static void Info(object caller, object methodName, object message)
        {
            if (caller == null)
            {
                throw new ArgumentNullException("caller");
            }
            ILog logger = LogManager.GetLogger(caller.GetType().ToString());
            if ((logger != null) && logger.IsInfoEnabled)
            {
                logger.Info(GetMessage(methodName, message));
            }
        }

        public static void Initialize()
        {
            XmlConfigurator.Configure();
        }

        public static void Warn(object caller, object methodName, object message)
        {
            if (caller == null)
            {
                throw new ArgumentNullException("caller");
            }
            ILog logger = LogManager.GetLogger(caller.GetType().ToString());
            if ((logger != null) && logger.IsWarnEnabled)
            {
                logger.Warn(GetMessage(methodName, message));
            }
        }
    }
}
